#include "utilities.h"
#include "Bot.h"
#include "Camera.h"
#include "Club.h"
#include "Receiver.h"
#include "Table.h"
#include "Puck.h"
#include <iostream>

int main()
{
	// Ask user for driver
	//E_DRIVER_TYPE driverType = driverChoiceConsole();
	
	//if (driverType == EDT_COUNT) return 1;
		
	Receiver receiver;

	// Create device
	IrrlichtDevice* device = createDevice(/*driverType*/EDT_OPENGL,	
		dimension2du(800, 600), 16, false, false, false, &receiver);

	if (!device) return 1;
			
	device->setWindowCaption(L"Air Hockey - Irrlicht Engine");
	device->getCursorControl()->setVisible(false);

	// Pointers to the main objects
	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();

	enum GAME_STATE {MAIN_MENU = 0, PLAY, PAUSE};
	int gameState = PLAY;

	// Skydome
	ISceneNode* skydome=smgr->addSkyDomeSceneNode(
		driver->getTexture(L"Data/Background/background.bmp"), 64, 32);

	// Table
	Table table = Table(device);

	// Camera
	Camera camera = Camera(smgr, table.getNode());

	// Puck
	Puck puck = Puck(device, table.getNode());

	// Clubs
	array <Club> club;
	club.push_back(Club(device, table.getNode(), &puck, false));
	club.push_back(Club(device, table.getNode(), &puck, true));

	club[0].setPosition(vector3df(Table::SIZE.X/2-15, Table::SIZE.Y, 0.f));
	club[1].setPosition(vector3df(15-Table::SIZE.X/2, Table::SIZE.Y, 0.f));

	// Bot
	Bot bot = Bot(puck.getPosition());

	int horDir = Club::HOR_NO;
	int verDir = Club::VER_NO;

	u32 refreshTimer = 0;
	u32 keyboardTimer = 0;

	u32 deltaX, deltaY;
	
	u32 lastX = receiver.getMouseState().Position.X;
	u32 lastY = receiver.getMouseState().Position.Y;

	vector3df result;
	f32 speed;

	while (device->run())
	{
		if (device->isWindowActive())
		{
			driver->beginScene();

			switch (gameState)
			{
			case MAIN_MENU:
				{
					break;
				}
			case PLAY: 
				{
					if ((device->getTimer()->getTime() - refreshTimer) > 10) 
					{
						refreshTimer = device->getTimer()->getTime();

						if (receiver.isKeyDown(KEY_KEY_W)) 
						{
							verDir = Club::UP;
						}
						if (receiver.isKeyDown(KEY_KEY_S)) 
						{
							verDir = Club::DOWN;
						}
						if (receiver.isKeyDown(KEY_KEY_A)) 
						{
							horDir = Club::LEFT;
						}
						if (receiver.isKeyDown(KEY_KEY_D)) 
						{
							horDir = Club::RIGHT;
						}
						if(!receiver.isKeyDown(KEY_KEY_W) &&
							!receiver.isKeyDown(KEY_KEY_S)) 
						{
							verDir = Club::VER_NO;
						}
						if(!receiver.isKeyDown(KEY_KEY_A) &&
							!receiver.isKeyDown(KEY_KEY_D))
						{
							horDir = Club::HOR_NO;
						}
						
						deltaX = receiver.getMouseState().Position.X - lastX;
						deltaY = receiver.getMouseState().Position.Y - lastY;

						lastX = receiver.getMouseState().Position.X;
						lastY = receiver.getMouseState().Position.Y;

						vector2di dir = bot.getDirection(club[1].getPosition(), puck.getPosition());

						club[0].update(horDir, verDir, deltaX, deltaY);
						club[1].update(dir.Y, dir.X, 0, 0);

						if (club[0].collisionWithPuck(puck.getPosition(), result, speed))
						{
							puck.setCollisionResult(result, speed);
						}
						if (club[1].collisionWithPuck(puck.getPosition(), result, speed))
						{
							puck.setCollisionResult(result, speed);
						}

						puck.update();
					}					
					smgr->drawAll();
					break;
				}
			case PAUSE:
				{
					// Redraw all scene nodes and show 'Pause' on top of them
					smgr->drawAll();
			
					break;
				}
			} // switch (gameState)

			driver->endScene();
		}
	} // while (device->run())

	device->drop();
	
	return 0;
}